/**
 * 
 */
package com.acxiom.bsh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.InvalidResultSetAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.acxiom.bsh.dao.SigninDao;
import com.acxiom.bsh.logger.LoggerManager;

/**
 * @author luciyu
 *
 */
@Repository
public class SigninDaoImpl implements SigninDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.acxiom.bsh.dao.SigninDao#signin()
	 */
	@Override
	public int signin(String mixNick, String sellerName) {
		int result = 0;
		try {
			String sql = "INSERT INTO bsh_tmall.signin(mixNick,signinDate,sellerName,creationDateTime,updateDateTime,creationUID,updateUID) " + "VALUES (?,curdate(),?,now(),now(),'signin','signin')";
			result = jdbcTemplate.update(sql, mixNick,sellerName);
		} catch (DataAccessException e) {
			LoggerManager.getErrorLogger().error("signin  DataAccess Error:", e);
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.acxiom.bsh.dao.SigninDao#querySignin()
	 */
	@Override
	public List<Integer> querySignin(String mixNick) {
		String sql = "select distinct day(signinDate) as day from signin where mixNick=? and month(signinDate)=month(curdate()) and year(signinDate)=year(curdate()) order by signinDate";

		List<Integer> signinList = null;
		try {
			SqlRowSet set = jdbcTemplate.queryForRowSet(sql, mixNick);

			signinList = new ArrayList<Integer>();
			while (set.next()) {
				signinList.add(set.getInt("day"));
			}
		} catch (InvalidResultSetAccessException e) {
			LoggerManager.getErrorLogger().error("querySignin  InvalidResultSetAccess Error:", e);
		} catch (DataAccessException e) {
			LoggerManager.getErrorLogger().error("querySignin  DataAccess Error:", e);
		}
		return signinList;
	}

}
